/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.service.cud.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import net.iwsh.core.utils.IDGenerator;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.us.usws.biz.ResourceStorageReplacer;
import com.us.usws.dao.VideoDAO;
import com.us.usws.model.domain.video.Video;
import com.us.usws.model.domain.video.VideoFile;
import com.us.usws.service.cud.VideoCUDService;

/**
 * Service RO impl class
 * @author robot
 */
public class VideoCUDServiceImpl implements VideoCUDService {

	private static Log log = LogFactory.getFactory().getInstance(VideoCUDServiceImpl.class);

	/**
	 * DAO instance
	 */
	private VideoDAO dao = new VideoDAO();

	public Video getVideo(String uid) {
		return dao.getVideo(uid);
	}

	/**
	 * Insert one row.
	 */
	@Override
	public String addVideo(Video video) {
		return dao.insertVideo(video);
	}

	/**
	 * Insert one row.
	 */
	@Override
	public boolean addVideo(Video video, VideoFile videoFile) {

		/*
		 * create video file uid
		 */
		String videoFileUid = IDGenerator.getInstance().genUID();
		videoFile.setUid(videoFileUid);

		/*
		 * create video uid and set file uid for video
		 */
		String videoUid = IDGenerator.getInstance().genUID();
		video.setUid(videoUid);
		video.setFileUid(videoFileUid);

		String snapshotPath = videoFile.getSnapshot();
		String videoPath = videoFile.getVideoPath();
		try {
			if (StringUtils.isNotBlank(snapshotPath)) {
				int index = snapshotPath.lastIndexOf("/");
				if (index != -1) {
					videoFile.setSnapshot(snapshotPath.substring(index + 1));
					FileUtils.copyFile(new File(snapshotPath),
							new File(ResourceStorageReplacer.getVideoFileSnapshotPath(videoFile.getUid(), videoFile.getSnapshot())));
				}
			}
			if (StringUtils.isNotBlank(videoPath)) {
				FileUtils.copyFile(new File(videoPath),
						new File(ResourceStorageReplacer.getVideoFilePath(videoFile.getUid(), videoFile.getFileExt())));
			}

			/*
			 * when file copy is success, then insert video info
			 */
			return dao.insertVideo(video, videoFile, video.getCatUids());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean addVideoFromTmp(Video video, VideoFile videoFile) {
		/*
		 * create video file uid
		 */
		String videoFileUid = IDGenerator.getInstance().genUID();
		videoFile.setUid(videoFileUid);

		String userUid = video.getCreateByUid();
		String tmpPath = ResourceStorageReplacer.getUserTmpDir(userUid);

		/*
		 * create video uid and set file uid for video
		 */
		String videoUid = IDGenerator.getInstance().genUID();
		video.setUid(videoUid);
		video.setFileUid(videoFileUid);

		String snapshotPath = videoFile.getSnapshot();
		String videoPath = videoFile.getVideoPath();
		try {
			if (StringUtils.isNotBlank(snapshotPath)) {
				File from = new File(tmpPath + "/" + snapshotPath);
				File to = new File(ResourceStorageReplacer.getVideoFileSnapshotPath(videoFile.getUid(), videoFile.getSnapshot()));
				log.info("Will copy snapshot file from [" + from.getAbsolutePath() + "] to [" + to.getAbsolutePath() + "].");
				FileUtils.copyFile(from, to);

			}
			if (StringUtils.isNotBlank(videoPath)) {
				File from = new File(tmpPath + "/" + videoPath);
				File to = new File(ResourceStorageReplacer.getVideoFilePath(videoFile.getUid(), videoFile.getFileExt()));
				log.info("Will copy video file from [" + from.getAbsolutePath() + "] to [" + to.getAbsolutePath() + "].");
				FileUtils.copyFile(from, to);
			}

			/*
			 * when file copy is success, then insert video info
			 */
			return dao.insertVideo(video, videoFile, video.getCatUids());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * Update by PK.
	 */
	@Override
	public void updateVideo(Video video) {
		dao.updateVideo(video);
	}

	/**
	 * Update by PK.
	 */
	@Override
	public void updateVideo(Video video, VideoFile videoFile) {

		String snapshotPath = videoFile.getSnapshot();
		try {
			if (StringUtils.isNotBlank(snapshotPath) && snapshotPath.contains("/")) {
				int index = snapshotPath.lastIndexOf("/");
				if (index != -1) {
					videoFile.setSnapshot(snapshotPath.substring(index + 1));
					FileUtils.copyFile(new File(snapshotPath),
							new File(ResourceStorageReplacer.getVideoFileSnapshotPath(videoFile.getUid(), videoFile.getSnapshot())));
				}
			}
			String videoPath = videoFile.getVideoPath();
			if (StringUtils.isNotBlank(videoPath) && videoPath.contains("/")) {
				FileUtils.copyFile(new File(videoPath),
						new File(ResourceStorageReplacer.getVideoFilePath(videoFile.getUid(), videoFile.getFileExt())));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		dao.updateVideo(video, videoFile, video.getCatUids());
	}

	/**
	 * Delete row by PK.
	 */
	@Override
	public void deleteVideo(String uid) {
		dao.deleteVideo(uid);
	}

	@Override
	public void deleteVideoMore(String uids) {
		if (StringUtils.isNotBlank(uids)) {
			String[] uid = StringUtils.split(uids, ",");
			for (String id : uid) {
				deleteVideo(id);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.us.usws.service.cud.VideoCUDService#changeVideoStatus(java.lang.String, java.lang.String, java.lang.Integer)
	 */
	public void changeVideoStatus(String uids, String lastUpdateUid, Integer status) {
		if (StringUtils.isNotBlank(uids)) {
			Date now = new Date(System.currentTimeMillis());
			String[] uid = StringUtils.split(uids, ",");
			for (String id : uid) {
				Video v = getVideo(id);
				v.setAuthStatusDsid(status);
				v.setLastUpdByUid(lastUpdateUid);
				v.setLastUpdDate(now);
				updateVideo(v);
			}
		}
	}

}